package com.we.es.util;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;

import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.uxuexi.core.common.util.Util;

/**
 * 
 *  日期工具
 * <p>
 * @author   朱晓川
 * @Date	 2018年5月27日
 */
public class DateConvertUtil {
	/**
	* Logger for this class
	*/
	private static final Log log = Logs.get();

	/**UTC时区*/
	public static final TimeZone TIME_ZONE_UTC = TimeZone.getTimeZone("UTC");
	/**东八区*/
	public static final TimeZone TIME_ZONE_LOCAL = TimeZone.getTimeZone(ZoneId.of("GMT+8"));

	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss:SSS";
	public static final String UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

	private DateConvertUtil() {
	}

	/**
	 * UTC时间转为东八区区时
	 */
	public static Date utc2Local(String utc) {
		if (Util.isEmpty(utc)) {
			return null;
		}

		Date utcDate = null;
		try {
			SimpleDateFormat utcformat = new SimpleDateFormat(UTC_FORMAT);
			utcformat.setTimeZone(TIME_ZONE_UTC);
			utcDate = utcformat.parse(utc);
			SimpleDateFormat localFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
			localFormat.setTimeZone(TIME_ZONE_LOCAL);

			String localTime = localFormat.format(utcDate.getTime());
			return localFormat.parse(localTime);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
